package com.fengye.kafka.controller;

import cn.hutool.core.util.IdUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class KafkaController {

    private static final Logger logger = LoggerFactory.getLogger(KafkaController.class);

    @Autowired
    private KafkaTemplate<String, String> kafkaTemplate;

    @GetMapping("/send")
    public void sendMessage(@RequestParam("topic") String topic) {
        // String id = IdUtil.getSnowflake().nextIdStr();
        kafkaTemplate.send(topic, "message");
    }

    @Scheduled(cron = "0/1 * * * * ?")
    public void sendScheduledMessage() {
        kafkaTemplate.send("i18n-test", IdUtil.fastSimpleUUID());
        logger.info("Scheduled message sent to topic i18n-test");
    }

}
